package com.tools;

import com.tools.SycSqlite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PublicDBManager {

	public static class ItemTable {

		public static final String TB_NAME = "ItemTable";
		public static final String _ID = "_id";
		public static final String DATA = "date";
		public static final String KEY = "key";
		public static final String ICON = "icon";
		public static final String TYPE = "TYPE";
		public static final String EXTR = "EXTR";

	}

	private PublicDBManager(Context context) {
		mContext = context;
		initSqliteDB();
	}

	Context mContext;

	public static PublicDBManager getInstence(Context context) {

		if (mForumesDBManager == null) {
			mForumesDBManager = new PublicDBManager(context);
		}
		mForumesDBManager.mContext = context;
		return mForumesDBManager;

	}

	private void initSqliteDB() {
		mSqLiteDatabase = mContext.openOrCreateDatabase("public.db",
				Context.MODE_PRIVATE, null);
		mSqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS "
				+ ItemTable.TB_NAME + " (" + ItemTable._ID
				+ " INTEGER PRIMARY KEY autoincrement not null,"
				+ ItemTable.DATA + " text," + ItemTable.ICON + " text,"
				+ ItemTable.EXTR + " text," + ItemTable.TYPE + " text,"
				+ ItemTable.KEY + " text" + ")");

	}

	public void release() {
		mSqLiteDatabase.close();
		mForumesDBManager = null;
	}

	public SycSqlite getSycSqlite() {
		return new SycSqlite(mSqLiteDatabase);
	}

	public String[] getBufferIcon() {
		Cursor cursor = getSycSqlite().query(ItemTable.TB_NAME,
				new String[] { ItemTable.ICON }, ItemTable.ICON + "!='null'",
				null, null, null, null);

		String[] buffer = new String[cursor.getCount()];

		int index = 0;

		while (cursor.moveToNext()) {
			buffer[index] = cursor.getString(0);
			index++;
		}
		return buffer;

	}

	SQLiteDatabase mSqLiteDatabase;
	static PublicDBManager mForumesDBManager;
}
